import { notify } from './NotifyRegistryCenter';
import getIn from './getIn';
import { LIVE_STATUS_START } from '../business/live/api/constrants';
let workman_client_id;
let _wsUrl;
export function init(wsUrl) {
    _wsUrl = wsUrl;
}
const all_msg = {};
const ERROR_MAX = 5;
let errNumber = 0;
// 初始化鸭梨的长连接socket
export default function initYaLiWebSocket() {
    const fail = () => {
        workman_client_id = undefined;
        if (errNumber < ERROR_MAX) {
            errNumber += 1;
			notify('SOCET_CLOSE');
        } else {
            wx.showToast({
                title: '重连次数达到上限',
            });
            rj && rj();
        }
    };

    return new Promise((rs, rj) => {
        if (workman_client_id) {
            rs && rs(workman_client_id);
            return;
        }
        wx.connectSocket({
            url: _wsUrl,
            success(res) {
            },
        });
        wx.onSocketOpen((res) => {

        });
        wx.onSocketClose((res) => {
            fail();
			console.log(JSON.stringify(res));
        });
        wx.onSocketError((res) => {
            fail();
			console.log(JSON.stringify(res));
        });
        wx.onSocketMessage((res) => {
            // wx.showToast({ title: '收到消息' });
            if (res.data) {
                const _result = JSON.parse(res.data);
                // 防止同条消息重复操作
                if (_result.msgId && all_msg[_result.msgId]) {
                    return;
                } else if (_result.msgId) {
                    all_msg[_result.msgId] = true;
                }
                if (_result) {
                    switch (_result.type) {
                        case 'currentLiveProduct': {
							console.log('切换推荐产品', res);
                            notify('UPDATE_LIVES_CURRENT_PRODUCT', _result);
                            break;
                        }
                        case 'editProductPrice': {
							console.log('修改产品价格广播', res);
                            notify('UPDATE_PRODUCT_PRICE', _result);
                            break;
                        }
                        case 'addToCart': {
							console.log('加入购物车广播', res);
                            notify('ADD_TO_CART', _result);
                            break;
                        }
                        case 'addOrder': {
							console.log('下单广播', res);
                            notify('ADD_ORDER', _result);
                            break;
                        }
                        case 'anchorFollow': {
							console.log('关注广播', res);
                            notify('ANCHOR_FOLLOW', _result);
                            break;
                        }
                        case 'updateLiveStatus': {
							console.log('直播状态更新广播', res);
                            notify('UPDATE_LIVES_STATUS', _result);
                            break;
                        }
                        case 'updateLiveZan': {
							console.log('点赞广播', res);
                            notify('UPDATE_LIVES_ZAN', _result);
                            break;
                        }
                        case 'connect': {
							console.log('socket连接成功广播', res);
                            errNumber = 0;
                            if (res.data) {
                                const result = JSON.parse(res.data);
                                workman_client_id = getIn(result, ['data', 'client_id']);
                                rs(workman_client_id);
                            }
                            notify('SOCET_CONNECT', _result);
                            break;
                        }
                        case 'close': {
                            workman_client_id = undefined;
                            break;
                        }
                        default: break;
                    }
                }
            }
        });
    });
}

